import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { CommonService } from '../service/common.service';
/**
 * 路由守卫：用于控制访问资源路径时必须为登录状态
 */
@Injectable()
export class AuthorityGuard implements CanActivate, CanActivateChild {
    constructor(private commonService: CommonService, private router: Router) { }

    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): boolean {
        let url: string = state.url;
        return this.checkLogin(url);
    }

    canActivateChild(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): boolean {
        return this.canActivate(route, state);
    }

    checkLogin(url: string): boolean {
        let token = localStorage.getItem('token');
        if (null != token) {
            return true;
        } else {
            if (url === '/system/api/searchHumanName' || url === '/system/api/login' || url === '/system/api/template' || url === '/system/api/unitinfo/loadaddress' ) {
                return true;
            }
            this.router.navigate(['/login']);
            return false;
        }
    }
}
